'use strict';

const Service = require('egg').Service;
const SysMenusModel = require('../../model/SysMenusModel');
const sysMenusModel = new SysMenusModel();

/**
 * 创建者: rliang
 * 创建时间: 2019年08月26日 10:45:15
 * 功能描述: 菜单操作
 */
class SysMenusService extends Service {

  /**
   * 查询用户菜单树
   * @param {string} adminUserId 用户ID
   */
  async querySysMenusTreeByAdminUserId(adminUserId) {
    // 查询所有菜单列表
    const sysMenusList = await sysMenusModel.selectAllSysMenusListByAdminUserId(this.app, adminUserId);
    // this.logger.debug('查询到的菜单列表：', sysMenusList);
    if (sysMenusList) {
      return this.convertMenusTree(sysMenusList, '1');
    }
    return [];
  }

  async queryAuthoListByAdminUserId(adminUserId) {
    // 查询所有菜单列表
    const userControllerAuthoList = await sysMenusModel.selectUserAuthoListByAdminUserId(this.app, adminUserId, 'controller');
    const _userFunctionNameAuthoList = await sysMenusModel.selectUserAuthoListByAdminUserId(this.app, adminUserId, 'functionName');
    if (userControllerAuthoList && _userFunctionNameAuthoList) {
      const userFunctionNameAuthoList = _userFunctionNameAuthoList.join(';').split(';');
      return {
        userControllerAuthoList,
        userFunctionNameAuthoList,
      };
    }
    return null;
  }

  async queryRoleMenuUserRights(roleId) {
    const menus = await sysMenusModel.selectMenusListByRoleId(this.app, roleId);
    if (menus) {
      return menus;
    }
    return [];
  }

  async queryMenuIdByRoleId(roleId) {
    const menuIds = await sysMenusModel.selectMenuIdListByRoleId(this.app, roleId);
    if (menuIds) {
      return menuIds;
    }
    return [];
  }

  /**
   * 查询所有菜单树
   */
  async querySysMenusTree() {
    // 查询所有菜单列表
    const sysMenusList = await sysMenusModel.selectAllSysMenusList(this.app);
    return this.convertMenusTree(sysMenusList, '1');
  }

  /**
   * 将菜单列表转换成菜单树 数据
   * @param {[sysMenus]} sysMenusList 菜单数组
   * @param {int} rootId 根目录ID
   */
  convertMenusTree(sysMenusList, rootId) {
    // 查找根目录
    const rootMenu = sysMenusList.find(item => {
      return item.id == rootId;
    });
    if (!rootMenu) return {};
    // 菜单树对象初始化
    const sysMenusTree = {
      rootMenu
    };

    sysMenusTree.rootMenu.children = this.getChildrenMenus(sysMenusList, rootMenu.id);

    return sysMenusTree;
  }

  /**
   * 查找子节点菜单列表
   * @param {[sysMenus]} sysMenusList 菜单数组
   * @param {string} parentId 父节点ID
   */
  getChildrenMenus(sysMenusList, parentId) {
    const _childrenSysMenus = [];
    // 查找所有子节点
    const childrenSysMenus = sysMenusList.filter(item => {
      return item.parentId == parentId;
    });
    if (childrenSysMenus && childrenSysMenus.length > 0) {
      for (const sysMenu of childrenSysMenus) {
        // 如果菜单数据 1: 目录；2:页面  则还是有下一级菜单，需要递归查找
        if (sysMenu.type == 1 || sysMenu.type == 2) {
          sysMenu.children = this.getChildrenMenus(sysMenusList, sysMenu.id);
          _childrenSysMenus.push(sysMenu);
        } else {
          // 为了用户权限特殊处理一下
          if (sysMenu.rightsTypes) {
            sysMenu.rightsTypes = sysMenu.rightsTypes.split(',').map(item => parseInt(item));
          } else {
            sysMenu.rightsTypes = [];
          }
          _childrenSysMenus.push(sysMenu);
        }
      }
    }
    return _childrenSysMenus;
  }

  // 超级用户菜单添加
  async adminAddMenu(menuModel) {
    if (!menuModel.controller) {
      const fnIndex = await sysMenusModel.getMenuFunctionIndex(this.app);
      menuModel.controller = 'FN' + fnIndex;
    }
    const result = await sysMenusModel.adminAddMenu(this.app, menuModel);
    return result;
  }

  // 查询菜单详细信息
  async findMenuById(id) {
    return await sysMenusModel.findMenuById(this.app, id);
  }

  // 删除菜单
  async deleteMenu(menuId) {
    const result = await sysMenusModel.deleteById(this.app, menuId); // deleteMenu(this.app, MenuIds);
    return result;
  }

  // 更新菜单
  async updateMenu(menuInfo) {
    if (!menuInfo.controller) {
      const fnIndex = await sysMenusModel.getMenuFunctionIndex(this.app);
      menuInfo.controller = 'FN' + fnIndex;
    }
    return await sysMenusModel.updateById(this.app, menuInfo);
  }
}

module.exports = SysMenusService;